<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Config;

/**
 * 控制台
 *
 * @icon fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
        $seventtime = \fast\Date::unixtime('day', -7);
        $paylist = $createlist = [];
        for ($i = 0; $i < 7; $i++)
        {
            $day = date("Y-m-d", $seventtime + ($i * 86400));
            $createlist[$day] = mt_rand(20, 200);
            $paylist[$day] = mt_rand(1, mt_rand(1, $createlist[$day]));
        }
        $hooks = config('addons.hooks');
        $uploadmode = isset($hooks['upload_config_init']) && $hooks['upload_config_init'] ? implode(',', $hooks['upload_config_init']) : 'local';
        $addonComposerCfg = ROOT_PATH . '/vendor/karsonzhang/fastadmin-addons/composer.json';
        Config::parse($addonComposerCfg, "json", "composer");
        $config = Config::get("composer");
        $addonVersion = isset($config['version']) ? $config['version'] : __('Unknown');
        $countUser = \app\admin\model\User::count();
        $countGoods = \app\admin\model\goods\Goods::count();
        $countOrder = \app\admin\model\SpellOrder::count();
        $countMoney = \app\admin\model\User::sum('money');
        $daytime = strtotime(date("Y-m-d",time()));
        $regNum = \app\admin\model\User::where('createtime','>',$daytime)->count();
        $loginNum = \app\admin\model\User::where('logintime','>',$daytime)->count();
        $dayOrderNum = \app\admin\model\SpellOrder::where('createtime','>',$daytime)->count();
        $daiOrderNum = \app\admin\model\SpellOrder::where('orderStatus','in',[2,5])->count();
        $payDayPrice = \app\admin\model\SpellOrder::where('orderStatus','in',[2,3,4,5,6,7,9])->where('payTime','>',$daytime)->sum("payPrice");
        $returnDayPrice = \app\admin\model\spell\Returned::where('status','in',[1,2,3,4])->where('createtime','>',$daytime)->sum('returnPrice');
        $tuanCount = \app\admin\model\spell\Spell::where('start',1)->count();
        $tuanCountFinish = \app\admin\model\spell\Spell::where('start',1)->where('isFinished',1)->count();
        $payPrice = \app\admin\model\SpellOrder::where('orderStatus','in',[2,3,4,5,6,7,9])->sum("payPrice");
        $returnPrice = \app\admin\model\spell\Returned::where('status','in',[1,2,3,4])->sum('returnPrice');
        $agentUser = \app\admin\model\User::where('agent','>',0)->count();
        $agentOrder = \app\admin\model\agent\Order::where('orderStatus',2)->sum('payPrice');
        $rebatePrice = \app\admin\model\rebate\Rebate::where('type','in',[5,6,7])->sum("price");
        $withdrawPrice = \app\admin\model\user\Withdraw::where('status','in',[1,2])->sum('price');
        $this->view->assign([
            'totaluser'        => $countUser,
            'totalviews'       => $countGoods,
            'totalorder'       => $countOrder,
            'totalorderamount' => $countMoney,
            'todayuserlogin'   => $loginNum,
            'todayusersignup'  => $regNum,
            'todayorder'       => $dayOrderNum,
            'unsettleorder'    => $daiOrderNum,
            'sevendnu'         => $payDayPrice,
            'sevendau'         => $returnDayPrice,
            'paylist'          => $paylist,
            'createlist'       => $createlist,
            'addonversion'       => $addonVersion,
            'uploadmode'       => $uploadmode,
            "tuanCount"        => $tuanCount,
            "tuanCountFinish"  => $tuanCountFinish,
            "payPrice"         => $payPrice,
            "returnPrice"      => $returnPrice,
            "agentUser"        => $agentUser,
            "agentOrder"       => $agentOrder,
            "rebatePrice"      => $rebatePrice,
            "withdrawPrice"    => $withdrawPrice,
        ]);

        return $this->view->fetch();
    }

}
